﻿Public Class Provlepomenes
    Private ms As Base.Entities.Moira()
    Private stratef As Base.SortableCollection(Of Base.Entities.StaffProvlepe)
    Private staff As Base.SortableCollection(Of Base.Entities.Staff)
    Private serviceTypes() As Base.Entities.ServiceType
    Private provlep As Integer
    Private deleteThese As List(Of Integer)

    Private Sub Provlepomenes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ms = My.Connector.Client.Moires
        ListBox1.DataSource = ms
        ListBox1.DisplayMember = "Name"
        serviceTypes = My.Connector.Client.ServiceType
        Dim serviceTypesList As New List(Of Base.Entities.ServiceType)
        serviceTypesList.AddRange(serviceTypes)
        serviceTypesList.Add(New Base.Entities.ServiceType(0, "(empty)"))
        serviceTypes = serviceTypesList.ToArray
    End Sub

    Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
        Dim selectedMoires As New List(Of Base.Entities.Moira)

        For Each i In ListBox1.SelectedItems
            selectedMoires.Add(i)
        Next

        stratef = My.Connector.Client.Provlepomenes(selectedMoires.ToArray)
        staff = My.Connector.Client.StaffSelection(selectedMoires.ToArray)
        staff.Add(New Base.Entities.Staff)

        dgvProvlepomenes.Columns.Clear()
        dgvProvlepomenes.DataSource = stratef
        dgvProvlepomenes.Columns.Remove(dgvProvlepomenes.Columns("StaffId")) '.CellTemplate = New DataGridViewComboBoxCell
        dgvProvlepomenes.Columns.Remove(dgvProvlepomenes.Columns("ServiceType")) '.CellTemplate = New DataGridViewComboBoxCell
        dgvProvlepomenes.Columns("Id").Visible = False

        Dim staffColumn As New DataGridViewComboBoxColumn
        With staffColumn
            .DataPropertyName = "StaffId"
            .HeaderText = "Στρατεύσιμος"
            .DropDownWidth = 160
            .Width = 90
            .MaxDropDownItems = 4
            .FlatStyle = FlatStyle.Flat
            .DataSource = staff
            .ValueMember = "Id"
            .DisplayMember = "StaffName" '"Description"
        End With

        Dim serviceColumn As New DataGridViewComboBoxColumn
        With serviceColumn
            .DataPropertyName = "ServiceType"
            .HeaderText = "Υπηρεσία"
            .DropDownWidth = 160
            .Width = 90
            .MaxDropDownItems = 4
            .FlatStyle = FlatStyle.Flat
            .DataSource = serviceTypes
            .ValueMember = "Id"
            .DisplayMember = "Description"
        End With

        deleteThese = New List(Of Integer)
        dgvProvlepomenes.Columns.Insert(0, staffColumn)
        dgvProvlepomenes.Columns.Insert(1, serviceColumn)

        dgvProvlepomenes.DataSource = stratef

    End Sub

    'Private Sub dgvProvlepomenes_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgvProvlepomenes.DataError
    '    'MsgBox(e.Exception)
    'End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        My.Connector.Client.UpdateProvlepomenes(stratef, deleteThese.ToArray)
        btnGet_Click(sender, e)
    End Sub

    Private Sub dgvProvlepomenes_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvProvlepomenes.UserDeletingRow
        Dim deleteMe As Base.Entities.StaffProvlepe = CType(e.Row.DataBoundItem, Base.Entities.StaffProvlepe)
        deleteThese.Add(deleteMe.Id)
    End Sub


End Class